package top.sinfonia.demo.elasticsearch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.sinfonia.demo.elasticsearch.po.User;
import top.sinfonia.demo.elasticsearch.service.UserService;

import java.util.List;

/**
 * @author singoasher
 * @date 2018/4/25
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public ResponseEntity<User> create(@RequestBody UserCommand userCommand) {
        return ResponseEntity.ok(userService.create(userCommand.getName(), userCommand.getRemark()));
    }

    @PutMapping("/{id}")
    public ResponseEntity<User> createOrUpdate(@PathVariable("id") String id, @RequestBody UserCommand userCommand) {
        return ResponseEntity.ok(userService.createOrUpdate(id, userCommand.getName(), userCommand.getRemark()));
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> get(@PathVariable("id") String id) {
        return ResponseEntity.ok(userService.get(id));
    }

    @GetMapping
    public ResponseEntity<List<User>> list() {
        return ResponseEntity.ok(userService.list());
    }
}
